package WGH;

import java.util.*;

public class Room implements Comparable{
	HashSet<Room> CloseTo;
	RoomSize roomSize;
	String name;
	private Person p1 = null;
	private Person p2 = null;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Room(String r) {
		this.name= r;
		CloseTo = new HashSet<Room>();
		CloseTo.add(Room.this);
	}

	public RoomSize getRoomSize() {
		return roomSize;
	}

	public void setRoomSize(RoomSize roomSize) {
		this.roomSize = roomSize;
	}

	@Override
	public int compareTo(Object o) {
		Room room = (Room) o;
		return (this.name.compareTo(room.name));
	}
	@Override
	public String toString(){
		String s = "";
		s += String.format("room(%s)\n", this.getName());
		s += String.format(this.roomSize.toString().substring(0, this.roomSize.toString().length()-4).toLowerCase().concat("-room")+ "(%s)\n", this.getName());
		String t = "";
		for(Room r: CloseTo)	t += r.getName()+", ";
		if(t.length()>0)	t = t.substring(0,t.length() - 2);
		s += String.format("close(%s, {%s})\n", this.getName(),t);
		return s;
	}
	
	public boolean close(Room r){
		return this.CloseTo.contains(r);
	}
	
	public static boolean close(Room r1, Room r2){
		return r1.CloseTo.contains(r2);
	}

	public Person getP1() {
		return p1;
	}

	public void setP1(Person p1) {
		this.p1 = p1;
	}

	public Person getP2() {
		return p2;
	}

	public void setP2(Person p2) {
		this.p2 = p2;
	}
	
	public boolean assign(Person p){
		if(!p.isSharable()&&this.getP1()==null&&this.getP2()==null){
			setP1(p);
			setP2(p);
			return true;
		}else if(!p.isSharable()&&(this.getP1()!=null||this.getP2()!=null)){
			return false;
		}
		
		if(this.p1 == null){
			setP1(p);
			return true;
		}
		if(this.p2 == null){
			setP2(p);
			return true;
		}else	return false;
	}
	public Person removeP1(){
		if(this.getP1()!=null){
			Person p = this.getP1();
			setP1(null);
			return p;
		}else{
			return null;
		}
	}
	public Person removeP2(){
		if(this.getP2()!=null){
			Person p = this.getP1();
			setP1(null);
			return p;
		}else{
			return null;
		}
	}
}
